﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using Microsoft.Phone.Tasks;
using System.IO;
using Microsoft.Phone;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps.Platform;
using Microsoft.Phone.Controls.Maps;
using System.Collections.Generic;
using System.Windows.Threading;


namespace AOuSuisJeWP7
{
    
    public class NewEnigmaViewModel : ViewModelBase
    {
        #region ctors

        public NewEnigmaViewModel() { }

        #endregion

        #region private members
        
        WriteableBitmap capturedImage;
        String title;
        String description;
        String statusText;
        GeoCoordinateWatcher watcher;
        Location position;
        Visibility progressBarVisibility = Visibility.Collapsed;
        Visibility mapVisibility = Visibility.Visible;
        readonly CredentialsProvider credentialsProvider = new ApplicationIdCredentialsProvider(App.BingMapsKey);

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                // Use the Position property of the GeoCoordinateWatcher object to get the current location.
                Position = watcher.Position.Location;
               
                watcher.Stop();
            }
        }

        #endregion

        #region public properties

        public CredentialsProvider BingCredentials
        {
            get
            {
                return credentialsProvider;
            }
        }

        public WriteableBitmap CapturedImage
        {
            get 
            { 
                return capturedImage; 
            }
            set 
            {
                capturedImage = value;
                OnPropertyChanged(() => CapturedImage);
                OnPropertyChanged(() => ImageOk);
            }
        }

        public Visibility ProgressBarVisibility
        {
            get
            {
                return progressBarVisibility;
            }
            set
            {
                progressBarVisibility = value;
                OnPropertyChanged(() => ProgressBarVisibility);
            }
        }

        public Visibility MapVisibility
        {
            get
            {
                return mapVisibility;
            }
            set
            {
                mapVisibility = value;
                OnPropertyChanged(() => MapVisibility);
            }
        }

        public String Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
                OnPropertyChanged(() => Title);
            }
        }
        public Guid IdEnigma
        {
            get;
            set;
        }
        public String StatusText
        {
            get
            {
                return statusText;
            }
            set
            {
                statusText = value;
                OnPropertyChanged(() => StatusText);
            }
        }

        public String Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
                OnPropertyChanged(() => Description);
            }
        }

        public List<String> ValidationErrors { get; set; }

        public Location Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
                StatusText = "Location data is available.";
                OnPropertyChanged(() => Position);
            }
        }

        public bool ImageOk 
        { 
            get 
            { 
                return CapturedImage != null; 
            } 
        }
        public Stream StrPhoto
        {
            get;
            set;
        }
        #endregion

        #region public methods

        public void HandleCapture(Stream photo)
        {
            StrPhoto =photo ; 
            CapturedImage = PictureDecoder.DecodeJpeg(photo);
            StrPhoto.Position = 0;
        }

        public void FindPosition()
        {
            if (watcher == null)
            {
                watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
                watcher.MovementThreshold = 5;
                watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            }

            // Inform the user that the application will appear inactive while waiting for the service to start.
            StatusText = "Starting location service. This could take up to one minute.";

            MapVisibility = Visibility.Collapsed;
            ProgressBarVisibility = Visibility.Visible;

            // Use the synchronous TryStart method, specifying 60 seconds for the TimeSpan.
            // The next line of code will not execute until the service is started or until the time span has been exceeded.
            bool started = watcher.TryStart(true, TimeSpan.FromMilliseconds(5000));

            ProgressBarVisibility = Visibility.Collapsed;
            MapVisibility = Visibility.Visible;

            // Check to see if the service was started successfully.
            if (started)
            {
                // Check to see if location data is available.
                if (watcher.Status == GeoPositionStatus.Ready)
                {
                    StatusText = "Location data is available.";
                }
                else
                {
                    StatusText = "Location data is not currently available. Please try again later.";
                }
            }
            else
            {
                StatusText = "The location service could not be started.";
            }
        }

        public bool Validate()
        {
            bool validationOk = true;

            ValidationErrors = new List<string>();

            if (!ImageOk)
            {
                validationOk = false;
                ValidationErrors.Add("CapturedImage");
            }
            
            if(String.IsNullOrEmpty(Title))
            {
                validationOk = false;
                ValidationErrors.Add("Title");
            }

            if (Position == null)
            {
                validationOk = false;
                ValidationErrors.Add("Position");
            }

            return validationOk;
        }

      

        #endregion
    }
}
